package com.cch.android.activity.view;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cch.android.activity.ModelLandscapActivity;
import com.cch.android.activity.R;
import com.cch.android.imageutil.ImageUtil;
import com.cch.android.util.Constant;
import com.cch.android.util.MyLog;


public class EffectActivity extends Activity implements OnClickListener{
	 /**
	 * 图片view
	 */
	private ImageView mImageView;
	/**
	 * 初始化的图片
	 */
	private Bitmap mBitmap;
	/**
	 * 变化后的图片
	 */
	private Bitmap bmpSet;
	
	private Context context;
	/**
	 * 存放图片 view
	 */
	
	private HashMap<Integer,View> mapView = new HashMap<Integer,View>();
	/**
	 * 标记上次选择 默认第一个
	 */
	private int selectLast=0;
	
	private Button mEditBtn_1,mEditBtn_2,mEditBtn_3,effect_close,effect_save;
	
	private galleryAdapter ba;
	/**
	 * 标示那个button
	 */
	private long initBut;
	
	private UGallery gl;
	
	/**
	 * 图片效果
	 */
	private Bitmap effectBmp=null;
	
	/**
	 * 本地图片路径集合
	 */
	private ArrayList<String> pathes;
	private int[] imageIDs_lome={
			R.drawable.effects_0,R.drawable.lomoeffect_01,R.drawable.lomoeffect_02,R.drawable.lomoeffect_03,
			R.drawable.lomoeffect_04,R.drawable.lomoeffect_05,R.drawable.lomoeffect_06};
	private String[] nameIDs_lome;
//	private String[] nameIDs_lome={
//			"原图","黑条纹","炫彩LO","浮雕","复古","底片","光照","经典HDR","老照片",
//			"古铜色","牛皮纸","流金水月","哥特风","反转色","时光隧道"
//			};
	private int[] imageIDs_photo={
			R.drawable.effects_0,R.drawable.photoeffect_01,R.drawable.photoeffect_02,R.drawable.photoeffect_03,
			R.drawable.photoeffect_04,R.drawable.photoeffect_05,R.drawable.photoeffect_06};
	private String[] nameIDs_photo={
			"原图","淡雅","柔光","日系","阿宝色","黑白","古典","旧时光","暖洋洋",
			"粉红佳人"
			};
	
	private int[] imageIDs_fashione={
			R.drawable.effects_0,R.drawable.fashioneffect_01,R.drawable.fashioneffect_02,R.drawable.fashioneffect_03,
			R.drawable.fashioneffect_04,R.drawable.fashioneffect_05,R.drawable.fashioneffect_06,
			R.drawable.fashioneffect_07};
	private String[] nameIDs_fashione={
			"原图","亮红","日光","平安夜","飞雪","夜景","七彩光晕","星芒"
			};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.model_setimage_effect);
		context = this;
        Intent intent = getIntent();
        mBitmap = intent.getParcelableExtra("imageBmp");
		initView();
	}

	private void initView() {
		mEditBtn_1 = (Button)findViewById(R.id.setimage1_effect_1);
		mEditBtn_2 = (Button)findViewById(R.id.setimage1_effect_2);
		mEditBtn_3 = (Button)findViewById(R.id.setimage1_effect_3);
		effect_close = (Button)findViewById(R.id.setimage1_effect_colse);
		effect_save = (Button)findViewById(R.id.setimage1_effect_save);
		initBut = R.id.setimage1_effect_1;
		
		mEditBtn_1.setOnClickListener(this);
		mEditBtn_2.setOnClickListener(this);
		mEditBtn_3.setOnClickListener(this);
		effect_close.setOnClickListener(this);
		effect_save.setOnClickListener(this);
		
		nameIDs_lome=new String[]{
				getString(R.string.effect_a),getString(R.string.effect_b),getString(R.string.effect_c),
				getString(R.string.effect_d),getString(R.string.effect_e),getString(R.string.effect_f),getString(R.string.effect_g)};
		
		//编辑图片初始化
		mImageView = ((ImageView) findViewById(R.id.setimage1_effect_image));
		if (mBitmap != null)
			mImageView.setImageBitmap(mBitmap);

		//Gallery 初始化
		gl = (UGallery) this.findViewById(R.id.Gallery01);
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, 
                LayoutParams.WRAP_CONTENT);
		lp.setMargins(-(Constant.m_screenWidth/2+150), 0, 0, 0);
		gl.setLayoutParams(lp);
		 ba = new galleryAdapter();

		ba.addDataInt(imageIDs_lome);
		ba.addDataStr(nameIDs_lome);
		gl.setAdapter(ba);
		gl.setOnItemClickListener(lomeListener);

	}
	OnItemClickListener lomeListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			switch (arg2) {
			case 0://原图
				mImageView.setImageBitmap(mBitmap);
				break;
             case 1:
            	 MyLog.log("选择1");
            	 effectBmp = ImageUtil.stria(mBitmap);//黑色条纹
            	 if (effectBmp != null)
         			mImageView.setImageBitmap(effectBmp);
				break;
             case 2:
            	 effectBmp = ImageUtil.overlay(context,mBitmap,R.drawable.rainbow_overlay);//图片叠加 彩色
            	 if (effectBmp != null)
         			mImageView.setImageBitmap(effectBmp);
 				break;
             case 3:
            	 effectBmp = ImageUtil.emboss(mBitmap);//浮雕
            	 if (effectBmp != null)
         			mImageView.setImageBitmap(effectBmp);
 				break;
             case 4:
            	 effectBmp = ImageUtil.oldRemeber(mBitmap);//复古
            	 if (effectBmp != null)
         			mImageView.setImageBitmap(effectBmp);
 				break;
             case 5:
            	 effectBmp = ImageUtil.film(mBitmap);//底片
            	 if (effectBmp != null)
         			mImageView.setImageBitmap(effectBmp);
 				break;
             case 6:
            	 effectBmp = ImageUtil.sunshine(mBitmap);//光照
            	 if (effectBmp != null)
         			mImageView.setImageBitmap(effectBmp);
 				break;
             case 7:
 				
 				break;
             case 8:
 				
 				break;
             case 9:
 				
 				break;
             case 10:
 				
 				break;
             case 11:
 				
 				break;
			}
		}
	};
	OnItemClickListener photoListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			MyLog.log("上一次选择位置："+selectLast);
			MyLog.log("选择位置："+arg2);
////			Gallery gl = (Gallery) findViewById(R.id.Gallery01);
//			if(arg2!=0&&selectLast==0)mapView.get(0).setBackgroundResource(R.drawable.effect_0);
//			mapView.get(selectLast).setBackgroundResource(imageIDs_lome[selectLast]);
//			mapView.get(arg2).setBackgroundResource(imageIDs_lome[0]);
////			arg0.setBackgroundResource();
////			gl.setSelection(arg2);
//			selectLast = arg2;
		}
	};
	OnItemClickListener fashioneListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			MyLog.log("上一次选择位置："+selectLast);
			MyLog.log("选择位置："+arg2);
////			Gallery gl = (Gallery) findViewById(R.id.Gallery01);
//			if(arg2!=0&&selectLast==0)mapView.get(0).setBackgroundResource(R.drawable.effect_0);
//			mapView.get(selectLast).setBackgroundResource(imageIDs_lome[selectLast]);
//			mapView.get(arg2).setBackgroundResource(imageIDs_lome[0]);
////			arg0.setBackgroundResource();
////			gl.setSelection(arg2);
//			selectLast = arg2;
		}
	};
	@Override
	protected void onDestroy() {
		
		if (effectBmp != null) {
			effectBmp.recycle();
			System.gc();
			effectBmp=null;
		}
		if (mBitmap != null) {
			mBitmap.recycle();
			System.gc();
			mBitmap=null;
		}
		if (bmpSet != null) {
			bmpSet.recycle();
			System.gc();
			bmpSet=null;
		}
		super.onDestroy();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.setimage1_effect_1:
			if (initBut != R.id.setimage1_effect_1) {
				initBut = R.id.setimage1_effect_1;
				mEditBtn_1.setBackgroundResource(R.drawable.btn_farmeweak_label_c);
				mEditBtn_2.setBackgroundResource(R.drawable.btn_farmeweak_label_a);
				mEditBtn_3.setBackgroundResource(R.drawable.btn_farmeweak_label_a);
				ba.addDataInt(imageIDs_lome);
				ba.addDataStr(nameIDs_lome);
				ba.notifyDataSetChanged();
				gl.setOnItemClickListener(lomeListener);
			}
		
			break;
		case R.id.setimage1_effect_2:
			if (initBut != R.id.setimage1_effect_2) {
				initBut = R.id.setimage1_effect_2;
				mEditBtn_1.setBackgroundResource(R.drawable.btn_farmeweak_label_a);
				mEditBtn_2.setBackgroundResource(R.drawable.btn_farmeweak_label_c);
				mEditBtn_3.setBackgroundResource(R.drawable.btn_farmeweak_label_a);
				ba.addDataInt(imageIDs_photo);
				ba.addDataStr(nameIDs_photo);
				ba.notifyDataSetChanged();
				gl.setOnItemClickListener(photoListener);
			}

			break;
		case R.id.setimage1_effect_3:
			if (initBut != R.id.setimage1_effect_3) {
				mEditBtn_1.setBackgroundResource(R.drawable.btn_farmeweak_label_a);
				mEditBtn_2.setBackgroundResource(R.drawable.btn_farmeweak_label_a);
				mEditBtn_3.setBackgroundResource(R.drawable.btn_farmeweak_label_c);
				ba.addDataInt(imageIDs_fashione);
				ba.addDataStr(nameIDs_fashione);
				ba.notifyDataSetChanged();
				initBut = R.id.setimage1_effect_3;
				gl.setOnItemClickListener(fashioneListener);
			}
			break;
		case R.id.setimage1_effect_colse:
//			backToMain();
			finish();
			break;
		case R.id.setimage1_effect_save:
			saveToMain();
			break;
		}
	}
	class galleryAdapter extends BaseAdapter{
		private int[] dataInt;
		private String[] dataStr;
		
		private void addDataInt(int[] dataInt){
			if(this.dataInt!=null)this.dataInt=null;
			this.dataInt = dataInt;
		}
		
		private void addDataStr(String[] dataStr){
			if(this.dataStr!=null)this.dataStr=null;
			this.dataStr = dataStr;
		}

		@Override
		public int getCount() {
			return this.dataInt!=null? this.dataInt.length:0;
		}

		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			View view = View.inflate(context, R.layout.modle_setimage_effect_grallery_item, null);
			ImageView galleryImageIv = (ImageView)view.findViewById(R.id.setimage1_effect_gallery_image);
			TextView textIv = (TextView)view.findViewById(R.id.setimage1_effect_gallery_text);
			String textStr = dataStr[arg0];
			LinearLayout.LayoutParams  lp = new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, 
	                LayoutParams.WRAP_CONTENT);
			if(textStr.length()==2){
			
			    lp.setMargins(20, 0, 0, 0);
			
			}else if(textStr.length()==3){
				
				lp.setMargins(9, 0, 0, 0);
			}else if(textStr.length()>=4){
				lp.setMargins(4, 0, 0, 0);
			}
			
			textIv.setLayoutParams(lp);
			textIv.setText(textStr);
			galleryImageIv.setImageResource(dataInt[arg0]);
			galleryImageIv.setScaleType(ImageView.ScaleType.FIT_XY);
			
			mapView.put(arg0, galleryImageIv);
			
			return view;
		}
		
	}
	
	private void backToMain(){
		Intent intent = new Intent();
		intent.putExtra("imageBmp", mBitmap);
		intent.setClass(this, ModelLandscapActivity.class);
		startActivity(intent);
	}
	/**
	 * 保存到sd卡 回到美化图片主界面
	 */
	private void saveToMain(){
		finish();
		Intent intent = new Intent();
		intent.putExtra("imageBmp", getSetBitmap());
		intent.setClass(this, ModelLandscapActivity.class);
		startActivity(intent);
	}
	/**
	 * 得到处理的图片
	 * @return
	 */
	private Bitmap getSetBitmap(){
		if (mImageView != null) {
			if (bmpSet != null) {
				bmpSet.recycle();
				System.gc();
				bmpSet=null;
			}
			bmpSet = Constant.drawable2Bitmap(mImageView.getDrawable());
			return bmpSet;
		}
		return bmpSet;
	}
}
